/**
 * 
 */
package com.hw.hcit.feature;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.hw.hcit.R;

import android.graphics.Color;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.hw.hcit.conf.Config;
import com.hw.hcit.conf.JCtrlCallback;
import com.hw.hcit.conf.JTestItem;

/**
 * @author gaoshengpan
 *
 */
public class SimCardsTestItem extends JTestItem {
    private TextView resultView,card1,card2;
    private static boolean flagSim1 = false;
    private static boolean flagSim2 = false;
    private static boolean isMult = true;
    TelephonyManager mtm ;
    private int stateSim1,stateSim2 ;
    private boolean tested = false;
    private String TAG = "SimCardsTestItem";
    private Button mButton1,mButton2;
    private boolean mmitested;

    /**
     * @param cb
     */
    public SimCardsTestItem(JCtrlCallback cb) {
        super(cb);
        // TODO Auto-generated constructor stub
    }

    /**
     * 
     */
    public SimCardsTestItem() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mtm = TelephonyManager.getDefault();
        
       
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.msimcards_test, container, false);
        resultView = (TextView) rootView.findViewById(R.id.textView1);
        card1 = (TextView) rootView.findViewById(R.id.textView2);
        card2 = (TextView) rootView.findViewById(R.id.textView3);
        mButton1 = (Button) rootView.findViewById(R.id.button1);
		mButton2 = (Button) rootView.findViewById(R.id.button2);
		mButton1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(25, 'P');
					} else {
						rewriteOneByteOfNv2499(56, 'P');
					}
				}
				 testFinish(true, 1, true);
				
			}
		});
		mButton2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(Config.CURR_PROJECT.isAuto()){
					if (!Config.ageingTested) {
						rewriteOneByteOfNv2499(25, 'F');
					} else {
						rewriteOneByteOfNv2499(56, 'F');
					}
				}
				testFinish(false, 1, false);
				
			}
		});
        return rootView;
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        tested = false;
        mButton1.setEnabled(false);
        mButton2.setEnabled(false);
        String mmi = readfile("persist/mmi_result.txt");
        if(mmi !=null && !"".equals(mmi) && mmi.length()== 64){
        	char[] c = mmi.toCharArray();
        	mmitested = c[62]=='P' ;
        	Log.d("XX", "simmmitested----->"+mmitested);
        }
        if(resultView == null){
            Log.d(TAG, "--------------->resultView == null");
        }
        if(card1 == null){
            Log.d(TAG, "--------------->card1 == null");
        }
        if(card1 == null){
            Log.d(TAG, "--------------->card2 == null");
        }
        if (mtm.isMultiSimEnabled() && !mmitested) {
            isMult = true;
            int stateSim1 = mtm.getSimState(0);
            int stateSim2 = mtm.getSimState(1);
            Log.d(TAG, "stateSim1------------->"+stateSim1);
            Log.d(TAG, "stateSim2------------->"+stateSim2);
            flagSim1 = isCardOK(stateSim1);
            flagSim2 = isCardOK(stateSim2);
            
            if(flagSim1 && flagSim2){
            	resultView.setText(R.string.test_success);
            	card1.setText(getString(R.string.card1_result)+getString(R.string.card_result_success));
            	card2.setText(getString(R.string.card2_result)+getString(R.string.card_result_success));
                resultView.setTextColor(Color.GREEN);
                card1.setTextColor(Color.GREEN);
                card2.setTextColor(Color.GREEN);
                Log.d(TAG, "2------------->"+flagSim1);
               /* if(Config.CURR_PROJECT.isAuto()){
                	testFinish(true, 1, true);
                	tested = true;
                }*/
                mButton1.setEnabled(true);
                
            }else{
                
                resultView.setText(R.string.test_fail);
                resultView.setTextColor(Color.RED);
                if(flagSim1){
                    card1.setText(getString(R.string.card1_result)+getString(R.string.card_result_success));
                }else{
                    card1.setText(getString(R.string.card1_result)+getString(R.string.card_result_fail));
                    card1.setTextColor(Color.RED);
                }
                
                if(flagSim2){
                    card2.setText(getString(R.string.card2_result)+getString(R.string.card_result_success));
                    card2.setTextColor(Color.GREEN);
                }else{
                    card2.setText(getString(R.string.card2_result)+getString(R.string.card_result_fail));
                    card2.setTextColor(Color.RED);
                }
                Log.d(TAG, "1------------->"+flagSim1);
                mButton2.setEnabled(true);
//                testFinish(false, 0, false);
            }
        } else {
            isMult = false;
            int stateSim1 = mtm.getSimState();
            flagSim1 = isCardOK(stateSim1);
            card2.setVisibility(View.GONE);
            if (!flagSim1) {
                resultView.setText(R.string.test_fail);
                resultView.setTextColor(Color.RED);
                card1.setText(getString(R.string.card_result)
                        + getString(R.string.card_result_fail));
                card1.setTextColor(Color.RED);
            } else {
                card1.setVisibility(View.GONE);
                resultView.setText(R.string.test_success);
                resultView.setTextColor(Color.GREEN);
                mButton1.setEnabled(true);
//                testFinish(true, 0, true);
            }
        }
    }
    private  String readfile(String filePath) {
        StringBuilder txtContent = new StringBuilder();
        byte[] b = new byte[1];
        InputStream in = null;
        try {
            in = new FileInputStream(filePath);
            while (in.read(b) != -1) {
                String s  = new String(b);
                txtContent.append(s);
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return txtContent.toString();
    }
   
    public boolean isCardOK(int state){
        switch (state) {
        case TelephonyManager.SIM_STATE_UNKNOWN:
        case TelephonyManager.SIM_STATE_ABSENT:
            return false;
        case TelephonyManager.SIM_STATE_PIN_REQUIRED:
        case TelephonyManager.SIM_STATE_PUK_REQUIRED:
        case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
        case TelephonyManager.SIM_STATE_READY:
        	return true;
        default:
            return false;

        }
    }
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        return true;
    }
    /*@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
            dissmissPowerClicked();
            Log.d(TAG, " --------->KeyEvent.KEYCODE_VOLUME_DOWN");
            if (tested) {
                Log.d(TAG,
                        "tested --------->KeyEvent.KEYCODE_VOLUME_DOWN");
                testFinish(true, 1, true);
            }else{
            	 testFinish(false, 1, true);
            }
        } else if (event.getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
            dissmissPowerClicked();
            testFinish(true, -1, true);
        } 
        return true;
    }*/

}
